package com.nanjia.boot.product.sku.spec.value.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.product.sku.spec.value.model.form.SkuSpecValueForm;
import com.nanjia.boot.product.sku.spec.value.model.query.SkuSpecValueQuery;
import com.nanjia.boot.product.sku.spec.value.model.vo.SkuSpecValueVO;
import com.nanjia.boot.product.sku.spec.value.service.SkuSpecValueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * SKU与规格值关联前端控制层
 *
 * @author 周洋
 * @since 2025-07-10 16:36
 */
@Tag(name = "SKU与规格值关联接口")
@RestController
@RequestMapping("/api/v1/sku-spec-value")
@RequiredArgsConstructor
public class SkuSpecValueController  {

    private final SkuSpecValueService skuSpecValueService;

    @Operation(summary = "SKU与规格值关联分页列表")
    @GetMapping("/page")
    public PageResult<SkuSpecValueVO> getSkuSpecValuePage(SkuSpecValueQuery queryParams ) {
        IPage<SkuSpecValueVO> result = skuSpecValueService.getSkuSpecValuePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增SKU与规格值关联")
    @PostMapping
    public Result<Void> saveSkuSpecValue(@RequestBody @Valid SkuSpecValueForm formData ) {
        boolean result = skuSpecValueService.saveSkuSpecValue(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取SKU与规格值关联表单数据")
    @GetMapping("/{id}/form")
   //@PreAuthorize(@ss.hasPerm('sku.spec.value:sku-spec-value:edit')")
    public Result<SkuSpecValueForm> getSkuSpecValueForm(
        @Parameter(description = "SKU与规格值关联ID") @PathVariable Long id
    ) {
        SkuSpecValueForm formData = skuSpecValueService.getSkuSpecValueFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改SKU与规格值关联")
    @PutMapping(value = "/{id}")
   //@PreAuthorize(@ss.hasPerm('sku.spec.value:sku-spec-value:edit')")
    public Result<Void> updateSkuSpecValue(
            @Parameter(description = "SKU与规格值关联ID") @PathVariable Long id,
            @RequestBody @Validated SkuSpecValueForm formData
    ) {
        boolean result = skuSpecValueService.updateSkuSpecValue(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除SKU与规格值关联")
    @DeleteMapping("/{ids}")
   //@PreAuthorize(@ss.hasPerm('sku.spec.value:sku-spec-value:delete')")
    public Result<Void> deleteSkuSpecValues(
        @Parameter(description = "SKU与规格值关联ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = skuSpecValueService.deleteSkuSpecValues(ids);
        return Result.judge(result);
    }
}
